package cn.edu.neu.softlab633.influencemaximization.sy.HighDegree_Algorithm;

import cn.edu.neu.softlab633.influencemaximization.sy.bean.Graph;
import cn.edu.neu.softlab633.influencemaximization.sy.bean.HighDegree.HighDegreeEle;
import cn.edu.neu.softlab633.influencemaximization.sy.bean.MarginGain;
import cn.edu.neu.softlab633.influencemaximization.sy.model.lt.LinearThresholdModel;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * Created by Jason on 2017/5/20.
 */
public class HighDegree {
    public static int getSeed(double threshold, Double[] query, int k, Graph graph) throws IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter("data/result_HighDegree_WZ_" + threshold + ".txt"));
        long start = System.currentTimeMillis();
        PriorityQueue<HighDegreeEle> highDegreeEles = new PriorityQueue<>();
        for (Map.Entry<Integer, Map<Integer, Double[]>> integerMapEntry : graph.getOutgoingIndex().getIndex().entrySet()) {
            int id = integerMapEntry.getKey();
            int outdegree = integerMapEntry.getValue().size();
            highDegreeEles.add(new HighDegreeEle(id, outdegree));
        }
        Set<Integer> seed = new HashSet<>();
        for (int i = 0; i < k; i++) {
            if (seed.size() % 10 == 0) {
                long end = System.currentTimeMillis();
                MarginGain tmp = LinearThresholdModel.influencePropagation(threshold, query, seed, graph);
                System.out.println("HighDegree:    k = " + seed.size());
                bw.write("HighDegree:    k = " + seed.size());
                bw.newLine();
                bw.write("    influence spread: " + tmp.getMarginGainNum());
                bw.newLine();
                bw.write("    running time: " + (end - start) + "ms");
                bw.newLine();
                System.out.println("    influence spread: " + tmp.getMarginGainNum());
                System.out.println("    running time: " + (end - start) + "ms");
            }
            seed.add(highDegreeEles.poll().getId());
        }
        // calculate influence spread
        MarginGain influece_spread = LinearThresholdModel.influencePropagation(threshold, query, seed, graph);
        long end = System.currentTimeMillis();
        System.out.println("HighDegree:    k = " + seed.size());
        bw.write("HighDegree:    k = " + seed.size());
        bw.newLine();
        bw.write("    influence spread: " + influece_spread.getMarginGainNum());
        bw.newLine();
        bw.write("    running time: " + (end - start) + "ms");
        bw.newLine();
        System.out.println("    influence spread: " + influece_spread.getMarginGainNum());
        System.out.println("    running time: " + (end - start) + "ms");
        bw.close();
        return influece_spread.getMarginGainNum();
    }
}
